package br.com.kliptus.web.jsf.managedbean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.maverick.persistence.filter.FindByExample;
import org.maverick.web.jsf.managedbean.ManagedBeanBase;
import org.maverick.web.jsf.managedbean.Paginator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import br.com.kliptus.model.bean.AddressJB;
import br.com.kliptus.model.bean.CityJB;
import br.com.kliptus.model.bean.ContactJB;
import br.com.kliptus.model.bean.CustomerJB;
import br.com.kliptus.model.bean.EntityJB;
import br.com.kliptus.model.bean.EntityTypeJB;
import br.com.kliptus.model.bean.FederalUnitJB;
import br.com.kliptus.model.enums.EntityTypeEnum;
import br.com.kliptus.persistence.dao.CustomerDAO;

@ManagedBean(name = "customerMB")
@ViewScoped
public class CustomerMB extends ManagedBeanBase {

	private static final long serialVersionUID = 1L;
	
	private CustomerJB customer;
	private CustomerJB customerSearch;
	private List<CustomerJB> customerList;
	
	private List<SelectItem> entityTypeList;
	private List<SelectItem> federalUnitList;
	
	private String oncomplete;
	
	@Autowired
	@Qualifier("customerDAOImpl")
	private CustomerDAO dao;

	public CustomerMB() {
		super();
		initComponents();
	}
	
	@Override
	public void initComponents() {
		this.customerSearch = new CustomerJB();
		this.paginator = new Paginator(this);
		list();
		buildEntityTypeList();
		buildFederalUnitList();
	}
	
	private void buildEntityTypeList() {
		
		this.entityTypeList = new ArrayList<SelectItem>();
		
		List<EntityTypeJB> list = this.dao.getEntityTypeList();
		
		for (EntityTypeJB entityType : list) 
			this.entityTypeList.add(new SelectItem(entityType, entityType.getDescription()));
		
	}
	
	private void buildFederalUnitList() {
		
		this.federalUnitList = new ArrayList<SelectItem>();
		
		List<FederalUnitJB> list = this.dao.getFederalUnitList();
		
		for (FederalUnitJB federalUnit : list) 
			this.federalUnitList.add(new SelectItem(federalUnit, federalUnit.getName()));
		
	}

	public void openModal(ActionEvent event) {
		this.customer = new CustomerJB();
		this.customer.setEntity(new EntityJB());
		this.customer.getEntity().setContact(new ContactJB());
		this.customer.getEntity().setAddress(new AddressJB());
		this.customer.getEntity().getAddress().setCity(new CityJB());
		this.customer.getEntity().getAddress().getCity().setFederalUnit(new FederalUnitJB());
		this.customer.getEntity().setEntityType(this.dao.getEntityTypeById(new Long(EntityTypeEnum.CORPORATE.getId())));
	}
	
	@Override
	public void save(ActionEvent event) {
		try {
			this.oncomplete = "dialogForm.hide();";
			this.dao.save(this.customer);
			list();
		} catch (Exception e) {
			this.oncomplete = "";
			e.printStackTrace();
		}
	}
	
	public void entitySelected() {
		System.out.println("-> entitySelected: [id = " + this.customer.getId() + "]");
	}

	@Override
	public void remove(ActionEvent event) {
		try {
			this.dao.remove(this.customer);
			list();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void list() {
		this.paginator.update();
	}
	
	@Override
	public Long count() {
		return this.dao.count(FindByExample.create(this.customerSearch));
	}

	@Override
	public void find(Integer firstResult, Integer maxResult) {
		this.customerList = this.dao.find(FindByExample.create(this.customerSearch).setFirstResult(firstResult).setMaxResults(maxResult));
	}

	public CustomerJB getCustomer() {
		return customer;
	}

	public void setCustomer(CustomerJB customer) {
		this.customer = customer;
	}

	public CustomerJB getCustomerSearch() {
		return customerSearch;
	}

	public void setCustomerSearch(CustomerJB customerSearch) {
		this.customerSearch = customerSearch;
	}

	public List<CustomerJB> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<CustomerJB> customerList) {
		this.customerList = customerList;
	}

	public List<SelectItem> getEntityTypeList() {
		return entityTypeList;
	}

	public void setEntityTypeList(List<SelectItem> entityTypeList) {
		this.entityTypeList = entityTypeList;
	}

	public String getOncomplete() {
		return oncomplete;
	}

	public void setOncomplete(String oncomplete) {
		this.oncomplete = oncomplete;
	}

	public List<SelectItem> getFederalUnitList() {
		return federalUnitList;
	}

	public void setFederalUnitList(List<SelectItem> federalUnitList) {
		this.federalUnitList = federalUnitList;
	}
	
}